package edu.eas.admin.service.impl;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageHelper;
import edu.eas.admin.mapper.OauthClientDetailsMapper;
import edu.eas.admin.service.OauthClientDetailsService;
import edu.eas.common.dto.ClientParam;
import edu.eas.common.pojo.OauthClientDetails;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author cql
 */
@Service
public class OauthClientDetailsServiceImpl implements OauthClientDetailsService {

    @Autowired
    private OauthClientDetailsMapper oauthClientDetailsMapper;


    @Override
    public List<OauthClientDetails> getList(Integer pageSize, Integer pageNum, String clientId, String authorizedGrantTypes) {
        PageHelper.startPage(pageNum, pageSize);
        return oauthClientDetailsMapper.getList(clientId, authorizedGrantTypes);
    }

    @Override
    public OauthClientDetails getOauthClientDetailByClientId(String clientId) {
        return oauthClientDetailsMapper.getOauthClientDetailByClientId(clientId);
    }

    @Override
    public int create(ClientParam param) {
        // 封装基本信息
        OauthClientDetails clientDetails = new OauthClientDetails();
        BeanUtils.copyProperties(param, clientDetails);
        // 1.生成客户端ID
        clientDetails.setClientId(IdUtil.simpleUUID());
        // 2.生成客户端密钥
        clientDetails.setClientSecret(IdUtil.simpleUUID());
        // 3.基本信息
        clientDetails.setCreateTime(new Date());
        clientDetails.setScope(String.join(",", param.getScope()));
        clientDetails.setAuthorizedGrantTypes(String.join(",", param.getAuthorizedGrantTypes()));
        // 4.时间转换
        if (clientDetails.getRefreshTokenValidity() != null) {
            clientDetails.setRefreshTokenValidity(clientDetails.getRefreshTokenValidity() * 3600 * 24);
        }
        if (clientDetails.getAccessTokenValidity() != null) {
            clientDetails.setAccessTokenValidity(clientDetails.getAccessTokenValidity() * 3600);
        }
        return oauthClientDetailsMapper.insert(clientDetails);
    }

    @Override
    public int delete(String clientId) {
        return oauthClientDetailsMapper.delete(clientId);
    }

    @Override
    public int update(ClientParam param) {
        // 封装基本信息
        OauthClientDetails clientDetails = new OauthClientDetails();
        BeanUtils.copyProperties(param, clientDetails);
        clientDetails.setScope(String.join(",", param.getScope()));
        clientDetails.setAuthorizedGrantTypes(String.join(",", param.getAuthorizedGrantTypes()));
        // 4.时间转换
        if (clientDetails.getRefreshTokenValidity() != null) {
            clientDetails.setRefreshTokenValidity(clientDetails.getRefreshTokenValidity() * 3600 * 24);
        }
        if (clientDetails.getAccessTokenValidity() != null) {
            clientDetails.setAccessTokenValidity(clientDetails.getAccessTokenValidity() * 3600);
        }
        return oauthClientDetailsMapper.update(clientDetails);
    }
}
